package com.genius.unite_platform.device.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.menu.service.MenuUtils;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.role.entity.RoleJson;
import com.genius.unite_platform.device.entity.DeviceEntity;
import com.genius.unite_platform.device.service.DeviceServiceAdapter;
import com.genius.workflow.page.PageResult;
import com.genius.workflow.page.SqlLang;
import com.genius.workflow.util.*;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;

@Service("DeviceSelectService")
public class DeviceSelectServiceImpl  extends DeviceServiceAdapter {
	@Override
	public JSONObject getSelectBody(JSONObject json, JSONObject response) {
		int limit = json.getIntValue(PageResult.limit);
        int page = json.getIntValue(PageResult.page);        
        String name="";
        if (json.containsKey("search"))
        {	
        	name=	(json.getJSONObject("search").getString("devicename")==null?"":json.getJSONObject("search").getString("devicename"));       
        }
        SqlLang sql = null;
        String selectSB = "select * from device where  devicename like '%"+name+"%'";  
        String headField = json.getString(PageResult.headField);
        selectSB = DBTools2.addParameter(selectSB, json);
        if (json.containsKey(PageResult.asc)) {
            String ascField = json.getString(PageResult.ascField);
            String asc = json.getString(PageResult.asc);
            sql = DBTools2.getSelectPageSql(headField, selectSB, page, limit, ascField, asc, null);
        } else {
            String asc = "asc";
            String ascField = "deviceno";
            sql = DBTools2.getSelectPageSql(headField, selectSB, page, limit, ascField, asc, null);
        }
        try {
//            String parameterArr [] = DBTools2.parsePermeterBySql(sql.getSourceSql(), n);
            System.out.println(sql);
            PageResult result = DBTools2.selectByCount(sql, null);
            JSONArray data = (JSONArray) result.getData();
            response.put(ResponseJson.count, result.getCount());
            response.put(ResponseJson.data, data);
            response.put(ResponseJson.status, ResponseJson.status_Success);

        }catch (ConnectionException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
        } catch (DBClosedException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
        } catch (SQLException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
        }
        return response;
    }
}





